package com.realidfarm.blewatch;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.util.Log;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import android.bluetooth.BluetoothDevice;

import com.manridy.sdk.Watch;
import com.manridy.sdk.callback.BleConnectCallback;
import com.manridy.sdk.type.InfoType;
import com.manridy.sdk.callback.BleCallback;
import com.manridy.sdk.exception.BleException;
import com.manridy.sdk.scan.TimeScanCallback;

public class BLEWatch extends CordovaPlugin {
    private Watch watch;
    private CallbackContext context;
    private List<SelectWatch> watchList = new ArrayList<>();
    private String curMac;

    public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
        Activity activity = this.cordova.getActivity();
        context = callbackContext;
        if (action.equals("init")) {
            curMac = args.getString(0);
            watch = Watch.getInstance(activity.getApplicationContext());
            scanDevice();
            callbackContext.success("init success");
            return true;
        } else if (action.equals("getInfo")) {
            watch.getSportInfo(InfoType.CURRENT_INFO, mBleCallback);
            watch.getSleepInfo(InfoType.HISTORY_INFO, mBleCallback);
            watch.getHeartRateInfo(InfoType.CURRENT_INFO, mBleCallback);
            watch.getBloodPressureInfo(InfoType.CURRENT_INFO, mBleCallback);
            watch.getBloodOxygenInfo(InfoType.CURRENT_INFO, mBleCallback);
            return true;
        }
        return false;
    }

    private void scanDevice() {
        watch.startScan(new TimeScanCallback(10000,"") {
            @Override
            public void onScanEnd() {
                Log.d("blewatch", "on Scan End");
            }

            @Override
            public void onFilterLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
                boolean isRepeat = false;
                for (int i = 0; i < watchList.size(); i++) {
                    if(watchList.get(i).getDevice().getAddress().equals(device.getAddress())){
                        isRepeat = true;
                    }
                }
                if (!isRepeat) {
                    watchList.add(new SelectWatch(device,rssi));
                    Log.d("blewatch", device.toString());
                    if(device.getAddress().equals(curMac)){
                        Log.d("blewatch", "Connecting");
                        watch.connect(device, true, new BleConnectCallback() {
                            @Override
                            public void onConnectSuccess() {
                                Log.d("blewatch", "Connect Success");
                            }

                            @Override
                            public void onConnectFailure(BleException e) {
                                Log.d("blewatch", e.toString());
                            }
                        });
                    }
                }
            }
        });
    }

    @NonNull
    private BleCallback mBleCallback = new BleCallback() {
        @Override
        public void onSuccess(Object o,String deviceMac) {
            String result = o.toString();
            PluginResult r = new PluginResult(PluginResult.Status.OK, result);
            r.setKeepCallback(true);
            context.sendPluginResult(r);
            Log.d("blewatch", result);
        }

        @Override
        public void onFailure(BleException e,String deviceMac) {
            String result = e.getDescription();
            Log.d("blewatch", result);
        }
    };
}



